package com.boomkeeper.mail.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;

import freemarker.template.TemplateExceptionHandler;

@Configuration
public class MailConfig {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(MailConfig.class);

	@Bean
	public freemarker.template.Configuration freemarkerConfig() {
		freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22);
		File templateFile = null;
		try {
			templateFile = ResourceUtils.getFile("classpath:templates/");
		} catch (FileNotFoundException e) {
			LOGGER.error("没有../resources/templates/这个路径无法完成初始化配置", e);
			System.exit(0);
		}
		
		try {
			configuration.setDirectoryForTemplateLoading(templateFile);
			configuration.setDefaultEncoding("UTF-8");
			configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
		} catch (IOException e) {
			LOGGER.error("初始化freemarker配置异常", e);
			System.exit(0);
		}
		
		return configuration;
	}
}
